package com.example.demo.controller;

import com.example.demo.VO.Result;
import com.example.demo.entity.Blank;
import com.example.demo.entity.Choice;
import com.example.demo.entity.Judgment;
import com.example.demo.entity.User;
import com.example.demo.service.BlankService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Blank)表控制层
 *
 * @author hjl
 * @since 2020-11-30 22:43:02
 */
@CrossOrigin
@RestController
@RequestMapping("blank")
@Api(value="填空题",tags={"填空题操作接口"})
public class BlankController {

    /**
     * 服务对象
     */
    @Autowired
    private BlankService blankService;

    @GetMapping("selectOne")
    public Blank selectOne(Integer id) {
        return this.blankService.queryById(id);
    }

    /**
     * 根据属性查询 （当参数为空时候）查询所有
     *
     * @param blank
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Blank>> query(@RequestBody(required = false) Blank blank) {
        return ResponseEntity.ok(blankService.queryByExample(blank));
    }

    /**
     * 插入一条数据
     *
     * @param blank
     * @return
     */
    @PostMapping("add")
    public ResponseEntity<List<Blank>> add(@RequestBody Blank blank) {
        Blank blank1=new Blank();
        if (blank.getId()!=null){
            blank1=blankService.update(blank);
        }else{
            blank1=blankService.insert(blank);
        }
        return ResponseEntity.ok(blankService.queryByExample(new Blank()));
    }

    /**
     * 根据主键更新数据
     *
     * @param blank
     * @return
     */
    @PutMapping("update")
    public ResponseEntity<List<Blank>> update(@RequestBody Blank blank) {
      Blank blank1=  blankService.update(blank);
        return ResponseEntity.ok(blankService.queryByExample(new Blank()));
    }

    /**
     * 根据主键删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("del/{id}")
    public ResponseEntity<List<Blank>> delete(@PathVariable("id") Integer id) {
        blankService.deleteById(id);
        return ResponseEntity.ok(blankService.queryByExample(new Blank()));
    }

    @PostMapping("delAll")
    public ResponseEntity<List<Blank>> deleteAll(@RequestBody Integer[] id) {
        for (int i = 0; i < id.length; i++) {
            blankService.deleteById(id[i]);
        }
        return ResponseEntity.ok(blankService.queryByExample(new Blank()));
    }

    @PostMapping("findblankByCourseid")
    public Result findblankByCourseid(Integer courseId) {
        Blank blank=new Blank();
        blank.setObj2(courseId+"");
        return Result.success(blankService.queryByExample(blank));
    }
}